﻿<!--#include file="config.asp"-->
<%
''' SDCMS 支付宝同步通知
''' ==================================================================
''' 版权所有 http://www.sdcms.cn
''' ------------------------------------------------------------------
''' 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
''' 未经授权不允许对程序代码以任何形式任何目的的再发布。
''' ==================================================================
''' 编写: IT平民
''' 修改：IT平民 in 2014.07

	sub callback()
		dim notify_id:notify_id=sdcms.enhtml(sdcms.fget("notify_id",0))
		dim sign:sign=sdcms.enhtml(sdcms.fget("sign",0))
		dim orderid:orderid=sdcms.enhtml(sdcms.fget("out_trade_no",0))
		dim trade_status:trade_status=sdcms.enhtml(sdcms.fget("trade_status",0))
		dim total_fee:total_fee=sdcms.enhtml(sdcms.fget("total_fee",0))
		dim trade_no:trade_no=sdcms.enhtml(sdcms.fget("trade_no",0))
		if sdcms.strlen(notify_id)=0 or sdcms.strlen(sign)=0 or sdcms.strlen(orderid)=0 or sdcms.strlen(trade_status)=0 or sdcms.strlen(trade_no)=0 then
			sdcms.echo "充值失败(参数不完整)，<a href="""&webroot&""">返回网站</a>"
			exit sub
		end if
		dim states
		states=sdcms.gethttp("http://notify.alipay.com/trade/notify_query.do?partner="&appid&"&notify_id="&notify_id,"")
		if not(states="true") then
			sdcms.echo "充值失败(验证失败)，<a href="""&webroot&""">返回网站</a>"
			exit sub
		else
			if trade_status="TRADE_FINISHED" or trade_status="TRADE_SUCCESS" then
				dim data:data=sdcms.db.dbload(1,"id,isover,amount,userid","sd_user_payrecord","orderid='"&orderid&"' and payway='支付宝'","")
				if ubound(data)>=0 then
					if data(1,0)=0 then
						sdcms.db.dbupdate "sd_user_payrecord","id="&data(0,0)&"",array(array("isover",1,0,0))
						dim userid:userid=data(3,0)
						dim userdata:userdata=sdcms.db.dbload(1,"amount","sd_user","id="&userid&"","")
						if ubound(userdata)=0 then
							dim amount:amount=sdcms.getint(userdata(0,0),0)+data(2,0)
							sdcms.db.dbupdate "sd_user","id="&userid&"",array(array("amount",amount,10,0))
							sdcms.createusermoney array(array("amount",data(2,0),0,0),array("userid",userid,0,0),array("type",1,0,0),array("content","支付宝充值，交易号："&trade_no&"",0,1),array("createdate",sqltime,0,0))
						end if
					end if
				end if
			end if
			sdcms.echo "恭喜，您已经成功充值"&total_fee&"元，<a href="""&webroot&""">返回网站</a>"
		end if
	end sub

	callback()
	sdcms.db.dbclose
%>